package com.love_weinuo.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.lzy.okgo.callback.StringCallback;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.love_weinuo.R;
import com.love_weinuo.bean.LandListCallBackBean;
import com.love_weinuo.databinding.ActivityMyLandBinding;
import com.love_weinuo.ui.BaseActivity;
import com.love_weinuo.ui.GaodeMap2Activity;
import com.love_weinuo.ui.GaodeMapActivity;
import com.love_weinuo.ui.adapter.MyLandAdapter;
import com.love_weinuo.util.GsonUtil;
import com.love_weinuo.util.HTTPAPI;
import com.love_weinuo.util.LogUtil;
import com.love_weinuo.util.SpUtils;
import com.love_weinuo.util.dialog.InCommonDialog;
import com.love_weinuo.util.dialog.SelectMeasureTypeDialog;
import com.love_weinuo.util.listener.OnBHClickListener;
import com.love_weinuo.util.listener.OnDeleteClickListener;
import com.love_weinuo.util.listener.OnPindanPointListener;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by SensYang on 2019/03/01 10:01
 */

public class MyLandActivity extends BaseActivity {

    ActivityMyLandBinding binding;
    MyLandAdapter adapter;
    List<LandListCallBackBean.ResultEntity.ResEntity> strs = new ArrayList<>();
    String userId = (String) SpUtils.get("module_user_id", "");
    List<LandListCallBackBean.ResultEntity.ResEntity> list = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_my_land);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        add(this);
        getLand();
        setBack();
        setTitleText("我的土地");
        adapter = new MyLandAdapter(this, strs);
        binding.recycler.setLayoutManager(new LinearLayoutManager(this));
        binding.recycler.setAdapter(adapter);
        adapter.setOnPindanPointListener(new OnPindanPointListener() {
            @Override
            public void setOnPindanPointListener(int position) {
                if (!"0".equals(strs.get(position).getState())) {
                    showInCommonDialog("土地使用中，请选择其他土地");
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("module_land_id", strs.get(position).getId() + "");
                intent.putExtra("module_land_area", strs.get(position).getArea() + "");
                intent.putExtra("module_land_name", strs.get(position).getName());
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        adapter.setOnDeleteClickListener(new OnDeleteClickListener() {
            @Override
            public void setOnDeleteClickListener(int position) {
                adapter.setOnDeleteClickListener(new OnDeleteClickListener() {
                    @Override
                    public void setOnDeleteClickListener(final int position) {
                        InCommonDialog dialog = new InCommonDialog(MyLandActivity.this);
                        dialog.setTitleText("确认删除" + strs.get(position).getName() + "?");
                        dialog.show();
                        dialog.setOnBHClickListener(new OnBHClickListener() {
                            @Override
                            public void onConfirmClick() {
                                deleteLand(strs.get(position).getId() + "");
                            }

                            @Override
                            public void onCloseClick() {

                            }
                        });
                    }
                });
            }
        });

        binding.tvModuleAddLand.setOnClickListener(this);
        binding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        findProductList();
                    }
                }, 1000);
            }
        });

        binding.refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        findLastList();
                    }
                }, 1000);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvModuleAddLand:
                toAddLand();
                break;
            default:
        }
    }

    public void toAddLand() {
        SelectMeasureTypeDialog dialog = new SelectMeasureTypeDialog(this);
        dialog.show();
        dialog.setOnBHClickListener(new OnBHClickListener() {
            @Override
            public void onConfirmClick() {
                startActivity(new Intent(MyLandActivity.this, GaodeMap2Activity.class));
            }

            @Override
            public void onCloseClick() {
                startActivity(new Intent(MyLandActivity.this, GaodeMapActivity.class));
            }
        });
    }

    public void findProductList() {
        LogUtil.e("头部刷新");
        binding.refreshLayout.finishRefresh();
        adapter.notifyDataSetChanged();
    }

    public void findLastList() {
        LogUtil.e("尾部刷新");
        binding.refreshLayout.finishLoadmore();
        adapter.notifyDataSetChanged();
    }

    public void getLand() {
        HTTPAPI.getInstance().getLands(userId, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("土地列表获取成功" + s);
                LandListCallBackBean bean = GsonUtil.GsonToBean(s, LandListCallBackBean.class);
                try {
                    switch (bean.getErrcode()) {
                        case 0:
                            list = bean.getResult().getRes();
                            strs.clear();
                            strs.addAll(list);
                            adapter.notifyDataSetChanged();
                            if (strs.size() == 0) {
                                binding.refreshLayout.setVisibility(View.GONE);
                                binding.ilModuleNothing.setVisibility(View.VISIBLE);
                            } else {
                                binding.refreshLayout.setVisibility(View.VISIBLE);
                                binding.ilModuleNothing.setVisibility(View.GONE);
                            }
                            break;
                        case 1:
                            binding.refreshLayout.setVisibility(View.GONE);
                            binding.ilModuleNothing.setVisibility(View.VISIBLE);
                            InCommonDialog dialog = new InCommonDialog(MyLandActivity.this);
                            dialog.show();
                            dialog.setTitleText(bean.getMsg());
                            dialog.setOnBHClickListener(new OnBHClickListener() {
                                @Override
                                public void onConfirmClick() {
                                    toAddLand();
                                }

                                @Override
                                public void onCloseClick() {

                                }
                            });
                            break;
                        default:
                            LogUtil.e("nothing，没有操作");
                    }
                } catch (Exception e) {
                    LogUtil.e("土地列表解析失败");
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtil.e("获取个人土地失败" + e.getMessage());
                showInCommonDialog("获取个人土地失败");
                return;
            }
        });
    }

    public void deleteLand(String landId) {
        HTTPAPI.getInstance().deleteLand(userId, landId, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("删除土地成功" + s);
                getLand();
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtil.e("删除土地失败" + e.getMessage());
            }
        });
    }

}
